package cn.momoshenchi.service.impl;


import cn.momoshenchi.pojo.SysRolePermission;
import cn.momoshenchi.pojo.SysUser;
import cn.momoshenchi.pojo.SysUserRole;
import cn.momoshenchi.service.SysPermissionService;
import cn.momoshenchi.service.SysRolePermissionService;
import cn.momoshenchi.service.SysUserRoleService;
import cn.momoshenchi.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @version : 1.0
 * @author: momoshenchi
 * @date: 2021/5/11 - 14:10
 */
@Service
public class UserDetailServiceImpl implements UserDetailsService
{

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysRolePermissionService sysRolePermissionService;

    @Autowired
    private SysPermissionService sysPermissionService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException
    {
        SysUser sysUser = sysUserService.getOne(new QueryWrapper<SysUser>().eq("username", s));
        if (sysUser == null)
        {
            throw new UsernameNotFoundException("用户名不存在");
        }
        List<Long> sysRoleIdList = sysUserRoleService.list(new QueryWrapper<SysUserRole>().eq("user_id", sysUser.getUserId()))
                .stream().map(SysUserRole::getRoleId).collect(Collectors.toList());

        Set<Long> permissionIdSet = new HashSet<>();
        sysRoleIdList.forEach(item -> {
                    List<Long> permissionIdList = sysRolePermissionService.list(
                            new QueryWrapper<SysRolePermission>().eq("role_id", item))
                            .stream()
                            .map(i -> {
                                        Long t = i.getPermissionId();
                                permissionIdSet.add(t);
                                        return t;
                                    }
                            )
                            .collect(Collectors.toList());
                }
        );
        Set<SimpleGrantedAuthority> sysPermissions = sysPermissionService.listByIds(permissionIdSet)
                .stream().map(item->{
                    return new SimpleGrantedAuthority( item.getPermission());
                }).collect(Collectors.toSet());
        sysUser.setAuthorities(sysPermissions);
        return new User(sysUser.getUsername(),new BCryptPasswordEncoder().encode(sysUser.getPassword()),sysUser.getAuthorities());
    }

}
